package cloud.lysky.java.on.reflect;

import java.lang.reflect.InvocationTargetException;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @author luoly
 * @describe
 * @date 2022-09-22 14:26
 **/
public class DynamicsSupplier <T> implements Supplier<T> {

    private Class<T> type;
    public DynamicsSupplier(Class<T> type){
        this.type = type;
    }

    @Override
    public T get() {
        try {
            return type.getConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        /*generate: 生成一个无限连续的无序流，流中的元素由用户定义的supplier函数生成*/
        Stream.generate(new DynamicsSupplier<>(ID.class)).skip(10).limit(5).forEach(System.out::println);
    }

}

class ID{
    private static long counter;
    private final long id = counter++;

    public ID(){

    }

    @Override
    public String toString() {
        return Long.toString(id);
    }
}